import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:myflutterproject/routes.dart';
import 'package:myflutterproject/utils/overScrollBehavior.dart';
import 'package:myflutterproject/utils/toast.dart';

import 'flutterchinesedemo.dart';

/**
 * StatelessWidget 是无状态组件，状态不可变的 widget
 * StatefulWidget 是有状态组件，持有的状态可能在 widget 生命周期改变
 */
class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    /**
     * WillPopScope用于处理是否离开当前页面
     * DateTime.now().difference()计算差值
     */
    return  Scaffold(
        appBar: AppBar(
          title: Text('各种示例'),
          centerTitle: true,
        ),
        body: Center(
          child: ScrollConfiguration(
            behavior: OverScrollBehavior(),
            child: ListView(
              children: <Widget>[
                //Text
                FlatButton(
                  onPressed: () {
                    Navigator.pushNamed(context, 'new_text');
                  },
                  child: const Text(
                    'open text demo',
                    style: TextStyle(fontSize: 20),
                  ),
                  textColor: Colors.blue,
                ),
                //Scaffold
                ItemButton(
                    label: 'open Scaffold+MaterialApp demo',
                    router: scaffoldDemo),
                ItemButton(label: 'open Container demo', router: containerDemo),
                ItemButton(label: '计数器应用', router: counterDemo),
                ItemButton(label: 'image demo', router: imageDemo),
                ItemButton(label: 'list demo', router: listDemo),
                ItemButton(label: 'long list demo', router: longlistDemo),
                ItemButton(label: 'horizontal demo', router: horizonalDemo),
                ItemButton(label: 'raw demo', router: rowDemo),
                ItemButton(label: 'grid demo', router: griddemo),
                ItemButton(label: 'grid more demo', router: gridmoredemo),
                ItemButton(label: 'appbar demo', router: appbardemo),
                ItemButton(label: 'appbar tab demo', router: appbartabdemo),
                ItemButton(label: 'layout demo', router: layoutdemo),
                ItemButton(label: 'stack demo（层叠布局）', router: stackdemo),
                ItemButton(
                    label: 'raisedbutton demo', router: raisedbuttondemo),
                ItemButton(label: 'flatbutton demo', router: flatbuttondemo),
                ItemButton(label: 'gesture demo(手势处理)', router: gesturedemo),
                ItemButton(label: 'dismiss demo(列表删除)', router: dismissdemo),
                ItemButton(
                    label: 'switch and checkbx demo(单选/复选框)',
                    router: switchandcheckboxdemo),
                ItemButton(
                    label: 'textfield demo(输入框)',
                    router: textfielddemo),
                ItemButton(
                    label: 'form demo(表单)',
                    router: formdemo),
                ItemButton(
                    label: 'theme demo',
                    router: themedemo),
                ItemButton(
                    label: 'navigationbar demo(底部导航)',
                    router: navigationbardemo),
                ItemButton(
                    label: 'navigation tab demo(可滑动导航)',
                    router: navigationtabbardemo),
                ItemButton(
                    label: 'drawer demo(抽屉组件)',
                    router: drawerdemo),
                ItemButton(
                    label: 'progress demo(进度控件)',
                    router: progressdemo),
                ItemButton(
                    label: 'wrap demo(流式布局)',
                    router: wrapdemo),
                ItemButton(
                    label: 'aglin demo(对齐布局)',
                    router: aglindemo),
                ItemButton(
                    label: 'constrain demo(尺寸限制类和装饰容器)',
                    router: constraindemo),
                ItemButton(
                    label: 'transform demo(变换)',
                    router: transformdemo),
                ItemButton(
                    label: '仿美团页面',
                    router: followmeituandemo),
              ],
            ),
          ),
        ),
    );
  }
}

class ItemButton extends StatelessWidget {
  late final String label;
  late final String router;

  ItemButton({this.label: '', this.router: ''}) : super();

  @override
  Widget build(BuildContext context) => FlatButton(
        child: Text(
          label,
          style: TextStyle(fontSize: 20),
        ),
        textColor: Colors.blue,
        onPressed: () => Navigator.pushNamed(context, router),
      );
}
